package com.hurui.xiaoming.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hurui.xiaoming.http.ApiClient;
import com.hurui.xiaoming.http.ApiSevice;
import com.hurui.xiaoming.weight.ProgressTips;

import java.text.DecimalFormat;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/7/10.
 */

public abstract class BaseFragment extends Fragment implements DialogInterface.OnCancelListener{

    public static final int POST = 0 ;
    public static final int GET  = 1 ;

    public Context mContext ;

    private ProgressTips mExecProgress;

    public ApiSevice apiSevice = ApiClient.getModel().getService();

    public DecimalFormat   fnum;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getActivity() ;
        //保留两位小数
        fnum   =   new DecimalFormat("##0.00");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public abstract void initView();

    public abstract void initAction() ;


    public void onOpen(Intent intent) {
        this.startActivity(intent);

    }

    public void onOpenForResult(Intent intent, Integer requestCode) {
        this.startActivityForResult(intent, requestCode);
    }

    public void onFinish() {
        getActivity().finish();
    }

    public void onFinishForResult(Intent intent,Integer resultCode) {
        getActivity().setResult(resultCode, intent);
        onFinish();
    }


    public void netAction(String url , Map<String , String> parmas , int type){
        Call<Object> call = null ;
        switch (type){
            case GET:
                call = apiSevice.onHttpGetOption(url , parmas);
                break;
            case POST:
                call = apiSevice.onHttpGetOption(url , parmas);
                break;
            default:
                break;
        }

        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                if(response.isSuccessful()){
                    Object object = response.body() ;
                    if(object != null){
                        onHttpSuccess(object);
                    }else{
                        onHttpFail(response.code() , "body为null");
                    }
                }else{
                    onHttpFail(response.code() , "response不成功");
                }
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                onHttpFail( -1 , t.getMessage());
            }
        });
    }


    public void onHttpSuccess(Object object){};

    public void onHttpFail(int code , String msg){};


    public void onShowPd() {
        if(mExecProgress==null){
            mExecProgress = ProgressTips.show(getActivity() , this);
        }
    }

    public void onHidePd() {
        if(mExecProgress!=null){
            mExecProgress.dismiss();
            mExecProgress=null;
        }
    }

    public void onToast(String msg) {
        Toast.makeText(getActivity() , msg, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onCancel(DialogInterface dialog) {
        onHidePd();
    }
}
